/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.project.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import org.netbeans.api.project.Project;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.DynamicMenuContent;
import org.openide.awt.Mnemonics;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.ContextAwareAction;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.actions.Presenter;
import pl.put.dcs_modeler.fileTypeDataLoader.DCSComputingModelDataObject;
import pl.put.dcs_modeler.utils.DCSProjectUtil;
import pl.put.dcs_modeler.visualModelEditor.windowComponenets.CFMWindowTopComponent;
/**
 *
 * @author LukaszR
 */
public class OpenComputingModelAction extends AbstractAction implements ContextAwareAction
{
    public static Logger log = Logger.getLogger( OpenComputingModelAction.class.getName() );

    @Override
    public void actionPerformed( ActionEvent e )
    {
        assert false;
    }

    @Override
    public Action createContextAwareInstance( Lookup context )
    {
        return new ContextAction( context );
    }

    private boolean enable( Project p )
    {
        assert p != null;

        return DCSProjectUtil.isDCSProject( p );
    }

    private String labelFor( Project p )
    {
        assert p != null;
        return "&Open computing model";
    }

    private void perform( Project p )
    {
        assert p != null;
        try
        {
            FileObject projTmp = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/DCS/DCSComputingModel.dcsmodel" );
            FileObject fo = p.getProjectDirectory().getFileObject( ( String )projTmp.getAttribute( "pathToModel" ) );

            DCSComputingModelDataObject dCSComputingModelDataObject = ( DCSComputingModelDataObject )DCSComputingModelDataObject.find( fo );
            CFMWindowTopComponent win = CFMWindowTopComponent.findInstance( dCSComputingModelDataObject ); //new CFMWindowTopComponent( dCSComputingModelDataObject );
            win.open();
            win.requestActive();
        }
        catch ( DataObjectNotFoundException ex )
        {
            Exceptions.printStackTrace( ex );
        }
    }

    private final class ContextAction extends AbstractAction implements Presenter.Popup
    {

        private final Project p;

        public ContextAction( Lookup context )
        {
            Project _p = context.lookup( Project.class );
            p = ( _p != null && enable( _p ) ) ? _p : null;
        }

        public void actionPerformed( ActionEvent e )
        {
            perform( p );
        }

        public JMenuItem getPopupPresenter()
        {
            class Presenter extends JMenuItem implements DynamicMenuContent
            {

                public Presenter()
                {
                    super( ContextAction.this );
                }

                public JComponent[] getMenuPresenters()
                {
                    if ( p != null )
                    {
                        Mnemonics.setLocalizedText( this, labelFor( p ) );
                        return new JComponent[]
                                {
                                    this
                                };
                    }
                    else
                    {
                        return new JComponent[ 0 ];
                    }
                }

                public JComponent[] synchMenuPresenters( JComponent[] items )
                {
                    return getMenuPresenters();
                }
            }
            return new Presenter();
        }
    }
}
